﻿
Imports Microsoft.Windows.Design.Interaction

Class WPFSearchDataGridHeaderVisibleTask
    Inherits Task


    Private dragBinding, endDragBinding As InputBinding
    Private initialMargin As Thickness

    ' The DockPanelMarginTask constructor establishes mappings 
    ' between user inputs and commands. 
    Public Sub New()
        Dim beginDrag As New ToolCommand("BeginDrag")
        Dim drag As New ToolCommand("Drag")
        Dim endDrag As New ToolCommand("EndDrag")
        Dim resetMargins As New ToolCommand("ResetMargins")

        InputBindings.Add(New InputBinding( _
            beginDrag, _
            New ToolGesture(ToolAction.DragIntent, MouseButton.Left)))

        InputBindings.Add( _
            New InputBinding( _
                resetMargins, _
                New ToolGesture(ToolAction.DoubleClick, MouseButton.Left)))

        dragBinding = New InputBinding( _
            drag, _
            New ToolGesture(ToolAction.Move))

        endDragBinding = New InputBinding( _
            endDrag, _
            New ToolGesture(ToolAction.DragComplete))

        ToolCommandBindings.Add(New ToolCommandBinding(beginDrag, AddressOf OnBeginDrag))
        ToolCommandBindings.Add(New ToolCommandBinding(drag, AddressOf OnDrag))
        ToolCommandBindings.Add(New ToolCommandBinding(endDrag, AddressOf OnEndDrag))
        ToolCommandBindings.Add(New ToolCommandBinding(resetMargins, AddressOf OnResetMargins))

    End Sub

    Private Sub OnBeginDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs)
        Dim data As GestureData = GestureData.FromEventArgs(args)

        BeginFocus(data)
        InputBindings.Add(dragBinding)
        InputBindings.Add(endDragBinding)

        initialMargin = CType(data.ImpliedSource.Properties("Margin").ComputedValue, Thickness)

    End Sub

    Private Sub OnDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs)
        Dim data As MouseGestureData = MouseGestureData.FromEventArgs(args)
        Dim offX As Double = data.PositionDelta.X
        Dim offY As Double = data.PositionDelta.Y

        Dim newMargin As Thickness = initialMargin

        newMargin.Bottom += offY
        newMargin.Top += offY
        newMargin.Left += offX
        newMargin.Right += offX

        data.ImpliedSource.Properties("Margin").SetValue(newMargin)

    End Sub

    Private Sub OnEndDrag(ByVal sender As Object, ByVal args As ExecutedToolEventArgs)
        Description = "Adjust margin"
        Complete()

    End Sub

    Protected Overrides Sub OnCompleted(ByVal e As EventArgs)
        Cleanup()
        MyBase.OnCompleted(e)

    End Sub

    Protected Overrides Sub OnReverted(ByVal e As EventArgs)
        Cleanup()
        MyBase.OnReverted(e)

    End Sub

    Private Sub Cleanup()
        InputBindings.Remove(dragBinding)
        InputBindings.Remove(endDragBinding)

    End Sub

    Private Sub OnResetMargins(ByVal sender As Object, ByVal args As ExecutedToolEventArgs)
        Dim data As GestureData = GestureData.FromEventArgs(args)
        data.ImpliedSource.Properties("Margin").ClearValue()

    End Sub
End Class
